package com.hsnn.medstgmini.base.dict.service.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hsnn.medstgmini.base.dict.dao.DicAreaLogDao;
import com.hsnn.medstgmini.base.dict.model.DicAreaLog;
import com.hsnn.medstgmini.base.dict.model.form.DicAreaLogForm;
import com.hsnn.medstgmini.base.dict.service.DicAreaLogManager;
import com.hsnn.medstgmini.sys.dao.SysUserDao;
import com.hsnn.medstgmini.sys.model.SysUser;
import com.hsnn.medstgmini.util.DateUtil;
import com.hsnn.medstgmini.util.FormatDate;
import com.hsnn.medstgmini.util.Pagination;
import com.hsnn.medstgmini.util.form.LogDetailForm;

@Service("dicAreaLogManager")
public class DicAreaLogManagerImpl  implements DicAreaLogManager {
	@Autowired
	private DicAreaLogDao dicAreaLogDao;
	@Autowired
	private SysUserDao sysUserdao;
	
	/**
	 * 基础库 数据字典 地区字典维护记录
	 * @param page
	 * @return
	 */
	@Resource(name = "sessionFactory")
	private SessionFactory sessionFactory;
	
	public Pagination getLogData(Pagination page) {
		try {
		page = dicAreaLogDao.getLogData(page);
		List<HashMap<String, Object>> mapList = (List<HashMap<String, Object>>)page.getRows();
		
		for (int i = 0; i < mapList.size(); ++i) {
			HashMap<String, Object> map = mapList.get(i);
			
			//格式化详情
			String detail = (String)map.get("detail");
			String[] sde = detail.split(";");
			for (int j = 0; j < sde.length; ++j) {
				sde[j] = StringUtils.replace(sde[j], ":", "由");
				sde[j] = StringUtils.replace(sde[j], ",", "变为");
			}
			StringBuffer newDetail = new StringBuffer();
			for (int j = 0; j < sde.length; ++j) {
				newDetail.append(sde[j]);
				if (j != sde.length - 1) {
					newDetail.append("<br/>");
				}
			}
			map.put("detail", newDetail.toString());
			
			//格式化时间
			String date = DateUtil.format((Timestamp)map.get("literUpdDttm"), "yyyy-MM-dd HH:mm:ss");
			map.put("literUpdDttm", date);
		}
		}catch(Exception e) {
			e.printStackTrace();
		}
		return page;
	}
	
	@SuppressWarnings("unchecked")
	public Pagination getAllHis(Pagination page) {
		page = dicAreaLogDao.getAllHis(page);
		page.setRows(formatDic((List<DicAreaLog>)page.getRows()));
		return page;
	}
	
	/**
	 * 基础库 数据字典 地区字典 根据id查询地区 日志详情
	 * @param page
	 * @return
	 */
	@Override
	public Pagination getAreaLogDetail(Pagination page) {
		
		page = dicAreaLogDao.getLasLogInfo(page);
		List<Map<String, Object>> list = (List<Map<String, Object>>) page.getRows();
		return page;
		/*
		Map<String, Object> map = dicAreaLogDao.getAreaLogDetail(page);
		String data = map.get("dataDetail")==null?"":map.get("dataDetail").toString();
		if (StringUtils.isNotBlank(data)){
			Gson gson = new Gson();
			List<LogDetailForm> list = gson.fromJson(data, new TypeToken<List<LogDetailForm>>(){}.getType());
			page.setRows(list);
		}
		Map<String, String> info = new HashMap<String, String>();
		Map<String, Object> lastLog = dicAreaLogDao.getLasLogInfo(map.get("upd_datetime").toString(), map.get("area_id").toString());
		info.put("newUpdtime", map.get("upd_datetime").toString());
		info.put("oldUpdtime", lastLog.get("upd_datetime").toString());
		info.put("newUpduser", map.get("upd_user").toString());
		info.put("oldUpduser", lastLog.get("upd_user").toString());
		page.setConditions(info);
		return page;
		*/
	}
	
	public String addDicAreaLog(DicAreaLog data){
		return dicAreaLogDao.addDicAreaLog(data);
	}
	
	private List<DicAreaLogForm> formatDic(List<DicAreaLog> list) {
    	if (null != list && list.size() >0) {
    		List<DicAreaLogForm> flist = new ArrayList<DicAreaLogForm>();
    		for (int i = 0; i < list.size(); i ++) {
    			flist.add(formartForm(list.get(i)));
    		}
    		return flist;
    	}
    	return null;
    }
	
	private DicAreaLogForm formartForm(DicAreaLog data){
		if (null == data){
			return null;
		}
		DicAreaLogForm form = new DicAreaLogForm();
		form.setLogId(data.getLogId());
		form.setAreaId(data.getAreaId());
		form.setAreaName(data.getAreaName());
		form.setDetail(data.getDetail());
		
		//格式化detail信息
		String detail = form.getDetail();
		String[] sde = detail.split(";");
		for (int i = 0; i < sde.length; ++i) {
			sde[i] = StringUtils.replace(sde[i], ":", "由");
			sde[i] = StringUtils.replace(sde[i], ",", "变为");
		}
		StringBuffer newDetail = new StringBuffer();
		for (int i = 0; i < sde.length; ++i) {
			newDetail.append(sde[i]);
			if (i != sde.length - 1) {
				newDetail.append("<br/>");
			}
		}
		form.setDetail(newDetail.toString());
		
		form.setArFullName(data.getArFullName());
		form.setArShortName(data.getArShortName());
		if (null != data.getFather()) {
			form.setFatherAreaCode(data.getFather().getAreaid());
			form.setFatherAreaName(data.getFather().getAreaname());
		}
		
		form.setSortId(data.getSortId());
		form.setStatus(data.getStatus());
		SysUser user = sysUserdao.getUserById(data.getUpdUser());
		if (null != user){
			form.setUpdUser(user.getName());
		}
		form.setLiterUpdDttm(data.getUpdDatetime()==null?"":FormatDate.toFullFormat(data.getUpdDatetime()));
		return form;
	}

}